<?php

defined('BASEPATH') or exit('No direct script access allowed');
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of StatisticAnalysis
 *
 * @author Administrator
 */
class StatisticAnalysis extends MY_Controller {

    //put your code here
    function __construct() {
        parent::__construct();
        $this->load->model('ChatRecord');
        $this->load->library('CreateExcel');
    }

//    热点问题
    
    public function hotQuestion() {
        $records = $this->ChatRecord->getRecordForAnalisis();
        $records = $this->handleQuestion1($records);
        $records = $this->sortByCount($records);
        $data['unknownStrs'] = $records['unknownStrs'];
        $data['numbers'] = $records['numbers'];
        $data['stockCode'] = $records['stockCode'];
        $totalCount = count($records['data']);
        if ($totalCount % PAGE_SHOW_NUMBER == 0) {
            $totalPage = $totalCount / PAGE_SHOW_NUMBER;
        } else {
            $totalPage = intval($totalCount / PAGE_SHOW_NUMBER + 1);
        }
        $records = array_slice($records['data'], 0, PAGE_SHOW_NUMBER);
        $data['statistics'] = $records;
        $data['categorys'] = $this->getCategory();
        $pageInfo['totalCount'] = $totalCount;
        $pageInfo['totalPage'] = $totalPage; 
        $data['page'] = $pageInfo;
        $this->load->view('statisticAnalysis/hotQuestion', $data);
    }
    
    public function hotQuestionPage(){
        $showPage = 10;
        $pageNum = $_REQUEST['pageNum'];
        $pageSet = $_REQUEST['pageSet'];
        $skip = ($pageNum - 1) * $pageSet;
        $records = $this->ChatRecord->getRecordForAnalisis();
        $records = $this->handleQuestion1($records);
        $records = $this->sortByCount($records);
        $totalCount = count($records['data']);
        $data['unknownStrs'] = $records['unknownStrs'];
        $data['numbers'] = $records['numbers'];
        $data['stockCode'] = $records['stockCode'];
        $records = array_slice($records['data'], $skip, $pageSet);
        $data['statistics'] = $records;
        if ($totalCount % $pageSet == 0) {
            $totalPage = $totalCount / $pageSet;
        } else {
            $totalPage = intval($totalCount / $pageSet + 1);
        }
        
        $pageInfo['totalCount'] = $totalCount;
        $pageInfo['pageNum'] = $pageNum;
        $pageInfo['pageSet'] = $pageSet;
        $pageInfo['showPage'] = $showPage;
        $pageInfo['totalPage'] = $totalPage; 
        $data['page'] = $pageInfo;
        if($records){
            $d['msg'] = '查询成功！';
            $d['code'] = 1;
            $d['data'] = $data;
        }else{
            $d['msg'] = '未查询到数据！';
            $d['code'] = 0;
        }
        echo json_encode($d);
    }
    
    public function searchHotQuestion(){
        $pageNum = $_REQUEST['pageNum'];
        $pageSet = $_REQUEST['pageSet'];
        $records = $this->ChatRecord->searchQuestion($_REQUEST);
        $records = $this->handleQuestion1($records);
        $records = $this->sortByCount($records);
        $skip = ($pageNum - 1) * $pageSet;
        $totalCount = count($records['data']);
        $data['unknownStrs'] = $records['unknownStrs'];
        $data['numbers'] = $records['numbers'];
        $data['stockCode'] = $records['stockCode'];
        $records = array_slice($records['data'], $skip, $pageSet);
        $data['statistics'] = $records;
        if ($totalCount % $pageSet == 0) {
            $totalPage = $totalCount / $pageSet;
        } else {
            $totalPage = intval($totalCount / $pageSet + 1);
        }
        
        $pageInfo['totalCount'] = $totalCount;
        $pageInfo['pageNum'] = $pageNum;
        $pageInfo['pageSet'] = $pageSet;
        $pageInfo['showPage'] = 10;
        $pageInfo['totalPage'] = $totalPage; 
        $data['page'] = $pageInfo;
        if($records){
            $d['msg'] = '查询成功！';
            $d['code'] = 1;
            $d['data'] = $data;
        }else{
            $d['msg'] = '未查询到数据！';
            $d['code'] = 0;
        }
        echo json_encode($d);
    }
    
    public function exportHotQuestion(){
        $records = $this->ChatRecord->searchQuestion($_REQUEST);
        $records = $this->handleQuestion1($records);
        $unknownStrs = $records['unknownStrs'];
        $numbers = $records['numbers'];
        $stockCode = $records['stockCode'];
        $records = $records['data'];
        $excelTitle = array(
            '序  号',
            '用户问题',
            '提问时间',
            "来  源",
            '提问次数'
        );
        $width = array(10,20,20,10,10);
        $excelContent = array();
        /* 获取查询到的数据 */
        if ($records) {
            $SerialNumber = 1; // 序号
            foreach ($records as $k => $v) {
                if($unknownStrs&&$unknownStrs['count']>=$v['count']){
                    $excelContent[] = array(
                        $SerialNumber ++,
                        $unknownStrs['comment'],
                        $unknownStrs['ctime'],
                        $unknownStrs['category'],
                        $unknownStrs['count']
                    );
                    $unknownStrs = false;
                }
                if($numbers&&$numbers['count']>=$v['count']){
                    $excelContent[] = array(
                        $SerialNumber ++,
                        $numbers['comment'],
                        $numbers['ctime'],
                        $numbers['category'],
                        $numbers['count']
                    );
                    $numbers = false;
                }
                if($stockCode&&$stockCode['count']>=$v['count']){
                    $excelContent[] = array(
                        $SerialNumber ++,
                        '股票代码',
                        $stockCode['ctime'],
                        $stockCode['category'],
                        $stockCode['count']
                    );
                    $stockCode = false;
                }
                $excelContent[] = array(
                    $SerialNumber ++,
                    $v['comment'],
                    $v['ctime'],
                    $v['category'],
                    $v['count']
                );
            }
        }
        $startTime = empty($_REQUEST['startTime'])?'*':$_REQUEST['startTime'];
        $endTime = empty($_REQUEST['endTime'])?'*':$_REQUEST['endTime'];
        $this->createexcel->file_name=$startTime.'~'.$endTime.'热点问题排名';
        $this->createexcel->setProperties($excelTitle, $excelContent, $width);
        $this->createexcel->getExcel('explorer');
    }
    
    
    private function handleQuestion($records){
        $len = count($records['data']);
        $category = $this->getCategory();
        
        for($i = 0;$i < $len;$i++){
            if(empty($records[$i]))continue;
            foreach ($records as $k=>$record){
                if($k == $i)continue;
                if($this->isSameMean($records[$i]['comment'], $record['comment'])){
                    $records[$i]['count'] = $records[$k]['count']+$records[$i]['count'];
                    unset($records[$k]);
                }
            }
            $records[$i]['ctime'] = date('Y-m-d', $records[$i]['ctime']);
            $records[$i]['comment'] = htmlspecialchars($records[$i]['comment']);
            $records[$i]['category'] = empty($category[$records[$i]['category']])?'无此分类':$category[$records[$i]['category']];
        }
        return array_merge($records);
    }

    private function handleQuestion1($records, $allRecords=array()){
        $numbers = array();
        $stockCode = array();
        $unknownStrs = array();
        $category = $this->getCategory();
        foreach ($records as $i=>$record){
            $records[$i]['ctime'] = date('Y-m-d', $records[$i]['ctime']);
            $records[$i]['comment'] = htmlspecialchars($records[$i]['comment']);
            $records[$i]['category'] = empty($category[$records[$i]['category']])?'无此分类':$category[$records[$i]['category']];
            if (!empty($allRecords)) {
                $idArr = array();
                foreach ($allRecords as $key=>$val){
                    if ($val['comment'] == $records[$i]['comment']) {
                        $idArr[] = $val['id'];
                    }
                }
                $records[$i]['idStr'] = implode(',', $idArr);
                unset($idArr);
            }
            if(preg_match('/^[0-9]+$/', $records[$i]['comment'])){
                if(preg_match('/^[0-9]{6}$/', $records[$i]['comment'])){
                    if(empty($stockCode)){
                        $stockCode = $records[$i];
                    }else{
                        $stockCode['count'] = $stockCode['count']+$records[$i]['count'];
                    }
                    
                }else{
                    if(empty($numbers)){
                        $numbers = $records[$i];
                    }else{
                        $numbers['count'] = $numbers['count']+$records[$i]['count'];
                    }
                    
                }
                unset($records[$i]);
            }else
                if(!preg_match('/^[0-9]+$/', $records[$i]['comment'])&&preg_match('/^[^\x{4e00}-\x{9fa5}]+$/u', $records[$i]['comment'])){
                    if(empty($unknownStrs)){
                        $unknownStrs = $records[$i];
                    }else{
                        $unknownStrs['count'] = $unknownStrs['count']+$records[$i]['count'];
                    }
                    unset($records[$i]);
            }
       }
        
        return array('data'=> array_merge($records),'numbers'=>$numbers,'unknownStrs'=>$unknownStrs,'stockCode'=>$stockCode);
    }
    
    private function isSameMean($question1,$question2){
        $search = array('，',',','.','。','?','？','啊','呢','啦','呀');
        if(str_replace($search, '', $question1) == str_replace($search, '', $question2)){
            return true;
            //纯数字
        }else 
            if(preg_match('/^[0-9]+$/', $question1)&&preg_match('/^[0-9]+$/', $question2)){
            return true;
            //非汉字
        }else if(!preg_match('/^[0-9]+$/', $question1)&&!preg_match('/^[0-9]+$/', $question2)&&preg_match('/^[^\x{4e00}-\x{9fa5}]+$/u', $question1)&&preg_match('/^[^\x{4e00}-\x{9fa5}]+$/u', $question2)){
            return true;
        }else{
            return false;
        }
    }
    
    
    //    热点问题end
    
    //未匹配问题
    public function getNotMatchQues(){
        $records = $this->ChatRecord->getNotMatchQues();
        $allRecords = $this->ChatRecord->getAllNotMatchQues();
        $records = $this->handleQuestion1($records, $allRecords);
        $data['unknownStrs'] = $records['unknownStrs'];
        $data['numbers'] = $records['numbers'];
        $data['stockCode'] = $records['stockCode'];
        $totalCount = count($records['data']);
        if ($totalCount % PAGE_SHOW_NUMBER == 0) {
            $totalPage = $totalCount / PAGE_SHOW_NUMBER;
        } else {
            $totalPage = intval($totalCount / PAGE_SHOW_NUMBER + 1);
        }
        $records = array_slice($records['data'], 0, PAGE_SHOW_NUMBER);
        $data['statistics'] = $records;
        $data['categorys'] = $this->getCategory();
        $pageInfo['totalCount'] = $totalCount;
        $pageInfo['totalPage'] = $totalPage; 
        $data['page'] = $pageInfo;
        $this->load->view('statisticAnalysis/notMatchQues', $data);
    }
    
    public function getNotMatchQuesPage(){
        $showPage = 10;
        $pageNum = $_REQUEST['pageNum'];
        $pageSet = $_REQUEST['pageSet'];
        $skip = ($pageNum - 1) * $pageSet;
        $records = $this->ChatRecord->getNotMatchQues();
        $allRecords = $this->ChatRecord->getAllNotMatchQues();
        $records = $this->handleQuestion1($records, $allRecords);
        $data['unknownStrs'] = $records['unknownStrs'];
        $data['numbers'] = $records['numbers'];
        $data['stockCode'] = $records['stockCode'];
        $totalCount = count($records['data']);
        $records = array_slice($records['data'], $skip, $pageSet);
        $data['statistics'] = $records;
        if ($totalCount % $pageSet == 0) {
            $totalPage = $totalCount / $pageSet;
        } else {
            $totalPage = intval($totalCount / $pageSet + 1);
        }
        
        $pageInfo['totalCount'] = $totalCount;
        $pageInfo['pageNum'] = $pageNum;
        $pageInfo['pageSet'] = $pageSet;
        $pageInfo['showPage'] = $showPage;
        $pageInfo['totalPage'] = $totalPage; 
        $data['page'] = $pageInfo;
        if($records){
            $d['msg'] = '查询成功！';
            $d['code'] = 1;
            $d['data'] = $data;
        }else{
            $d['msg'] = '未查询到数据！';
            $d['code'] = 0;
        }
        echo json_encode($d);
    }
    
    public function searchNotMatchQues(){
        $pageNum = $_REQUEST['pageNum'];
        $pageSet = $_REQUEST['pageSet'];
        $records = $this->ChatRecord->searchNotMatchQues($_REQUEST);
        $allRecords = $this->ChatRecord->searchAllNotMatchQues($_REQUEST);
        $records = $this->handleQuestion1($records, $allRecords);
        $data['unknownStrs'] = $records['unknownStrs'];
        $data['numbers'] = $records['numbers'];
        $data['stockCode'] = $records['stockCode'];
        $totalCount = count($records['data']);
        $skip = ($pageNum - 1) * $pageSet;
        $data['statistics'] = $records;
        $records = array_slice($records['data'], $skip, $pageSet);
        $data['statistics'] = $records;
        if ($totalCount % $pageSet == 0) {
            $totalPage = $totalCount / $pageSet;
        } else {
            $totalPage = intval($totalCount / $pageSet + 1);
        }
        
        $pageInfo['totalCount'] = $totalCount;
        $pageInfo['pageNum'] = $pageNum;
        $pageInfo['pageSet'] = $pageSet;
        $pageInfo['showPage'] = 10;
        $pageInfo['totalPage'] = $totalPage; 
        $data['page'] = $pageInfo;
        if($records){
            $d['msg'] = '查询成功！';
            $d['code'] = 1;
            $d['data'] = $data;
        }else{
            $d['msg'] = '未查询到数据！';
            $d['code'] = 0;
        }
        echo json_encode($d);
    }

    public function statisticNotMatchQues(){
        $rows = $this->ChatRecord->getAllNotMatchRecords($_REQUEST);
        $categoryArr    = array();
        $quesArr        = array();
        $records = array();
        $total = 0;
        //总记录数
        $total = count($rows);
        foreach ($rows as $key => $row) {
            //统计分类
            if(!isset($categoryArr[$row['cate_group']])) {
                $categoryArr[$row['cate_group']] = array();
                $categoryArr[$row['cate_group']]['totalTimes'] = 1;
                $categoryArr[$row['cate_group']]['dealed'] = 0;
                $categoryArr[$row['cate_group']]['todeal'] = 0;
                $categoryArr[$row['cate_group']]['ignore'] = 0;
            } else {
                $categoryArr[$row['cate_group']]['totalTimes']++;
            }

            //统计每个分类下的问题数
            if (!isset($quesArr[$row['cate_group']])) {
                $quesArr[$row['cate_group']] = array();
                $quesArr[$row['cate_group']][] = $row['comment'];
                $categoryArr[$row['cate_group']]['quesAmount'] = 1;
            } else {
                if (!in_array($row['comment'], $quesArr[$row['cate_group']])) {
                    $quesArr[$row['cate_group']][] = $row['comment'];
                    $categoryArr[$row['cate_group']]['quesAmount']++;
                }
            }
            //统计该分类各个状态的记录的次数
            if ($row['is_add'] == 1) {
                $categoryArr[$row['cate_group']]['dealed']++;
                } else {
                if ($row['is_ignore'] == 1) {
                    $categoryArr[$row['cate_group']]['ignore']++;
                    } else {
                    $categoryArr[$row['cate_group']]['todeal']++;
                    }
                }
            }

        //占比统计
        foreach ($categoryArr as $key => $val) {
            $categoryArr[$key]['comampercent'] = number_format($categoryArr[$key]['quesAmount'] / $total * 100, 2) . "%";
            $categoryArr[$key]['catampercent'] = number_format($categoryArr[$key]['totalTimes'] / $total * 100, 2) . "%";
            $categoryArr[$key]['categoryname'] = $key;
        }
        //数组结构调整(冒泡排序)
        foreach ($categoryArr as $c) {
            $records[] = $c;
        }

        $totalCount = count($records);
        for ($i = 0; $i < $totalCount; $i++) {
            for ($j = 0; $j < $totalCount - $i - 1; $j++) {
                if ($records[$j]['totalTimes'] < $records[$j+1]['totalTimes']) {
                    $temp = array();
                    $temp = $records[$j];
                    $records[$j] = $records[$j+1];
                    $records[$j+1] = $temp;
                }
            }
        }
        if ($totalCount % PAGE_SHOW_NUMBER == 0) {
            $totalPage = $totalCount / PAGE_SHOW_NUMBER;
        } else {
            $totalPage = intval($totalCount / PAGE_SHOW_NUMBER + 1);
        }
        $records = array_slice($records, 0, PAGE_SHOW_NUMBER);
        $pageInfo['totalCount'] = $totalCount;
        $pageInfo['totalPage'] = $totalPage;
        $data['page'] = $pageInfo;
        $data['records'] = $records;
        $this->load->view('statisticAnalysis/statisticNotMatchQues', $data);
    }

    public function searchStatisticNotMatchQues(){
        $pageNum = $_REQUEST['pageNum'];
        $pageSet = $_REQUEST['pageSet'];
        $rows = $this->ChatRecord->getAllNotMatchRecords($_REQUEST);
        $categoryArr    = array();
        $quesArr        = array();
        $records = array();
        $total = 0;
        //总记录数
        $total = count($rows);
        foreach ($rows as $key => $row) {
            //统计分类
            if(!isset($categoryArr[$row['cate_group']])) {
                $categoryArr[$row['cate_group']] = array();
                $categoryArr[$row['cate_group']]['totalTimes'] = 1;
                $categoryArr[$row['cate_group']]['dealed'] = 0;
                $categoryArr[$row['cate_group']]['todeal'] = 0;
                $categoryArr[$row['cate_group']]['ignore'] = 0;
            } else {
                $categoryArr[$row['cate_group']]['totalTimes']++;
            }

            //统计每个分类下的问题数
            if (!isset($quesArr[$row['cate_group']])) {
                $quesArr[$row['cate_group']] = array();
                $quesArr[$row['cate_group']][] = $row['comment'];
                $categoryArr[$row['cate_group']]['quesAmount'] = 1;
            } else {
                if (!in_array($row['comment'], $quesArr[$row['cate_group']])) {
                    $quesArr[$row['cate_group']][] = $row['comment'];
                    $categoryArr[$row['cate_group']]['quesAmount']++;
                }
            }
            //统计该分类各个状态的记录的次数
            if ($row['is_add'] == 1) {
                $categoryArr[$row['cate_group']]['dealed']++;
                } else {
                if ($row['is_ignore'] == 1) {
                    $categoryArr[$row['cate_group']]['ignore']++;
                    } else {
                    $categoryArr[$row['cate_group']]['todeal']++;
                    }
                }
            }

        //占比统计
        foreach ($categoryArr as $key => $val) {
            $categoryArr[$key]['comampercent'] = number_format($categoryArr[$key]['quesAmount'] / $total * 100, 2) . "%";
            $categoryArr[$key]['catampercent'] = number_format($categoryArr[$key]['totalTimes'] / $total * 100, 2) . "%";
            $categoryArr[$key]['categoryname'] = $key;
        }

        //数组结构调整(冒泡排序)
        foreach ($categoryArr as $c) {
            $records[] = $c;
        }
        $totalCount = count($records);
        for ($i = 0; $i < $totalCount; $i++) {
            for ($j = 0; $j < $totalCount - $i - 1; $j++) {
                if ($records[$j]['totalTimes'] < $records[$j+1]['totalTimes']) {
                    $temp = array();
                    $temp = $records[$j];
                    $records[$j] = $records[$j+1];
                    $records[$j+1] = $temp;
                }
            }
        }
        $skip = ($pageNum - 1) * $pageSet;
        $records = array_slice($records, $skip, $pageSet);
        if ($totalCount % $pageSet == 0) {
            $totalPage = $totalCount / $pageSet;
        } else {
            $totalPage = intval($totalCount / $pageSet + 1);
        }
        $pageInfo['totalCount'] = $totalCount;
        $pageInfo['pageNum'] = $pageNum;
        $pageInfo['pageSet'] = $pageSet;
        $pageInfo['showPage'] = 10;
        $pageInfo['totalPage'] = $totalPage;
        $data['page'] = $pageInfo;
        $data['records'] = $records;
        if(count($records)>0){
            $d['msg'] = '查询成功！';
            $d['code'] = 1;
            $d['data'] = $data;
        }else{
            $d['msg'] = '未查询到数据！';
            $d['code'] = 0;
        }
        echo json_encode($d);
        exit();
    }
    
    public function exportNotMatchQues(){
        $records = $this->ChatRecord->searchNotMatchQues($_REQUEST);
        $records = $this->handleQuestion1($records);
        $unknownStrs = $records['unknownStrs'];
        $numbers = $records['numbers'];
        $stockCode = $records['stockCode'];
        $records = $records['data'];
        $excelTitle = array(
            '序号',
            '用户问题',
			'分类名称',
            '提问时间',
            '来源',
            '提问次数'
        );
        $width = array(10,20,20,10,10);
        $excelContent = array();
        /* 获取查询到的数据 */
        if ($records) {
            $SerialNumber = 1; // 序号
            foreach ($records as $k => $v) {
                if($unknownStrs&&$unknownStrs['count']>=$v['count']){
                    $excelContent[] = array(
                        $SerialNumber ++,
                        $unknownStrs['comment'],
						$unknownStrs['cate_group'],
                        $unknownStrs['ctime'],
                        $unknownStrs['category'],
                        $unknownStrs['count']
                    );
                    $unknownStrs = false;
                }
                if($numbers&&$numbers['count']>=$v['count']){
                    $excelContent[] = array(
                        $SerialNumber ++,
                        $numbers['comment'],
						$numbers['cate_group'],
                        $numbers['ctime'],
                        $numbers['category'],
                        $numbers['count']
                    );
                    $numbers = false;
                }
                if($stockCode&&$stockCode['count']>=$v['count']){
                    $excelContent[] = array(
                        $SerialNumber ++,
                        '股票代码',
						$stockCode['cate_group'],
                        $stockCode['ctime'],
                        $stockCode['category'],
                        $stockCode['count']
                    );
                    $stockCode = false;
                }
                $excelContent[] = array(
                    $SerialNumber ++,
                    $v['comment'],
					$v['cate_group'],
                    $v['ctime'],
                    $v['category'],
                    $v['count']
                );
            }
        }
        $startTime = empty($_REQUEST['startTime'])?'*':$_REQUEST['startTime'];
        $endTime = empty($_REQUEST['endTime'])?'*':$_REQUEST['endTime'];
        $this->createexcel->file_name=$startTime.'~'.$endTime.'未匹配问题排名';
        $this->createexcel->setProperties($excelTitle, $excelContent, $width);
        $this->createexcel->getExcel('explorer');
    }   
    
    public function getKnowledgePoint(){
        $records = $this->ChatRecord->getKnowledgePoint();
        $records = $this->handleQuestion1($records);
        $records = $this->sortByCount($records);
        $totalCount = count($records['data']);
        if ($totalCount % PAGE_SHOW_NUMBER == 0) {
            $totalPage = $totalCount / PAGE_SHOW_NUMBER;
        } else {
            $totalPage = intval($totalCount / PAGE_SHOW_NUMBER + 1);
        }
        $records = array_slice($records['data'], 0, PAGE_SHOW_NUMBER);
        $data['statistics'] = $records;
        $data['categorys'] = $this->getCategory();
        $pageInfo['totalCount'] = $totalCount;
        $pageInfo['totalPage'] = $totalPage; 
        $data['page'] = $pageInfo;
        $this->load->view('statisticAnalysis/knowledgePoint', $data);
    }
    
    public function getKnowledgePointPage(){
        $showPage = 10;
        $pageNum = $_REQUEST['pageNum'];
        $pageSet = $_REQUEST['pageSet'];
        $skip = ($pageNum - 1) * $pageSet;
        $records = $this->ChatRecord->getKnowledgePoint();
        $records = $this->handleQuestion1($records);
        $records = $this->sortByCount($records);
        $totalCount = count($records['data']);
        $records = array_slice($records['data'], $skip, $pageSet);
        $data['statistics'] = $records;
        if ($totalCount % $pageSet == 0) {
            $totalPage = $totalCount / $pageSet;
        } else {
            $totalPage = intval($totalCount / $pageSet + 1);
        }
        
        $pageInfo['totalCount'] = $totalCount;
        $pageInfo['pageNum'] = $pageNum;
        $pageInfo['pageSet'] = $pageSet;
        $pageInfo['showPage'] = $showPage;
        $pageInfo['totalPage'] = $totalPage; 
        $data['page'] = $pageInfo;
        if($records){
            $d['msg'] = '查询成功！';
            $d['code'] = 1;
            $d['data'] = $data;
        }else{
            $d['msg'] = '未查询到数据！';
            $d['code'] = 0;
        }
        echo json_encode($d);
    }
    
    public function searchKnowledgePoint(){
        $pageNum = $_REQUEST['pageNum'];
        $pageSet = $_REQUEST['pageSet'];
        $records = $this->ChatRecord->searchKnowledgePoint($_REQUEST);
        $records = $this->handleQuestion1($records);
        $records = $this->sortByCount($records);
        $skip = ($pageNum - 1) * $pageSet;
        $totalCount = count($records['data']);
        $data['statistics'] = $records;
        $records = array_slice($records['data'], $skip, $pageSet);
        $data['statistics'] = $records;
        if ($totalCount % $pageSet == 0) {
            $totalPage = $totalCount / $pageSet;
        } else {
            $totalPage = intval($totalCount / $pageSet + 1);
        }
        
        $pageInfo['totalCount'] = $totalCount;
        $pageInfo['pageNum'] = $pageNum;
        $pageInfo['pageSet'] = $pageSet;
        $pageInfo['showPage'] = 10;
        $pageInfo['totalPage'] = $totalPage; 
        $data['page'] = $pageInfo;
        if($records){
            $d['msg'] = '查询成功！';
            $d['code'] = 1;
            $d['data'] = $data;
        }else{
            $d['msg'] = '未查询到数据！';
            $d['code'] = 0;
        }
        echo json_encode($d);
    }
    
    public function exportKnowledgePoint(){
        $records = $this->ChatRecord->searchKnowledgePoint($_REQUEST);
        $records = $this->handleQuestion1($records);
        $records = $records['data'];
        $excelTitle = array(
            '序号',
            '用户问题',
            '提问时间',
            '来源',
            '提问次数'
        );
        $width = array(10,20,20,10,10);
        $excelContent = array();
        /* 获取查询到的数据 */
        if ($records) {
            $SerialNumber = 1; // 序号
            foreach ($records as $k => $v) {
                $excelContent[] = array(
                    $SerialNumber ++,
                    $v['comment'],
                    $v['ctime'],
                    $v['category'],
                    $v['count']
                );
            }
        }
        $startTime = empty($_REQUEST['startTime'])?'*':$_REQUEST['startTime'];
        $endTime = empty($_REQUEST['endTime'])?'*':$_REQUEST['endTime'];
        $this->createexcel->file_name=$startTime.'~'.$endTime.'知识点排名';
        $this->createexcel->setProperties($excelTitle, $excelContent, $width);
        $this->createexcel->getExcel('explorer');
    }

    public function executeUpdate(){
        if (empty($_REQUEST['startTime']) || empty($_REQUEST['endTime'])) {
            echo json_encode(array('code'=>0, 'msg'=>'开始和截止时间不能为空！'));
            exit();    
        }
        $startTime = strtotime($_REQUEST['startTime']);
        $endTime = strtotime($_REQUEST['endTime']);
        $signal = 1;
        $cin = array("startTime"=>$startTime, "endTime"=>$endTime, "signal"=>1);
        file_put_contents('/data/log/robot/questionTypeSignal.txt', json_encode($cin));
        echo json_encode(array('code'=>1, 'msg'=>'后台已收到更新请求，10分钟内处理完毕！'));
        exit();
    }

    public function dealWithThisComment(){
        $idStr = $_REQUEST['idStr'];
        $idArr = explode(',', $idStr);
        if (empty($idArr)) {
            echo json_encode(array('code'=>0, 'msg'=>'要更新的记录为空！'));
            exit();
        }
        foreach ($idArr as $id) {
            $this->ChatRecord->updateSingleField('is_add', 1, "id=".$id);
        }
        echo json_encode(array('code'=>1, 'msg'=>'关于该问题的该段时间内的记录已全部解决！'));
        exit();
    }
    
    public function ignoreThisComment(){
        $idStr = $_REQUEST['idStr'];
        $idArr = explode(',', $idStr);
        if (empty($idArr)) {
            echo json_encode(array('code'=>0, 'msg'=>'要更新的记录为空！'));
            exit();
        }
        foreach ($idArr as $id) {
            $this->ChatRecord->updateSingleField('is_ignore', 1, "id=".$id);
        }
        echo json_encode(array('code'=>1, 'msg'=>'关于该问题的该段时间内的记录已全部忽略！'));
        exit();
    }
    
    private function sortByCount($records){ 
//        for ($i = 0;$i<count($records['data']);$i++){
//            for ($k = $i+1;$k<count($records['data']);$k++){
//               if($records[$i]['count']<$records[$k]['count']){
//                   $temp = $records[$i];
//                   $records[$i] = $records[$k];
//                   $records[$k] = $temp;
//               } 
//            }
//        }
        return $records;
    }
    
    private function getCategory(){
        $category = file_get_contents('/data/www/html/zhangxihang/data/category.txt');
        $categoryArr = json_decode($category);
        return $categoryArr;
    }
}
